package com.vhly.kxmlext;

import com.vhly.kxmlext.outputer.XMLOutputter;
import org.kxml2.io.KXmlParser;
import org.kxml2.kdom.Document;
import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-4-8
 * Time: 9:52:17
 */
public class Main {
    public static void main(String[] args) {
        int argc = args.length;
        String xmlfile = "res/public.xml";
        if (argc == 1) {
            xmlfile = args[0];
        }

        try {
//            final Controller controller = new Controller();
//            controller.startAllocationRecording(true, 10, true, 0x100000);

            FileInputStream fin = null;
            long ct1 = System.currentTimeMillis();
            KXmlParser parser = new KXmlParser();
            Document dom = new Document();
            try {
                fin = new FileInputStream(xmlfile);
                parser.setInput(fin, null);
                parser.shouldProcessEntity(false);
                dom.parse(parser);
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                XMLOutputter outputter = new XMLOutputter();
                outputter.output(dom, bout);
                byte[] buf = bout.toByteArray();
                String xml = new String(buf, "UTF-8");
                System.out.println("xml = " + xml);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fin != null) {
                    try {
                        fin.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            long ct2 = System.currentTimeMillis();
            System.out.println("ct2-ct1 = " + (ct2 - ct1));
//            controller.stopAllocationRecording();
//            controller.captureMemorySnapshot();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
